<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #box{
            position: relative;
            width: 400px;
            height: 500px;
            margin: 0 auto;
            border: 2px solid red;
        }
        #pipe{
            position: absolute;
            top: 0;
            height: 500px;
            left: 300px;
            width: 50px;
        }
        #up{
            height: 300px;
            background: red;
            width: 100%;
        }
        #down{
            width: 100%;
            height: 100px;
            position: absolute;
            bottom: 0;
            background: blue;
        }
        #bird{
            position: absolute;
            width: 30px;
            height: 30px;
            background: blueviolet;
            top: 299px;
            left: 30px;
        }
    </style>
</head>
<body>
    <div id="box">
        <div id="bird"></div>
        <div id="pipe">
            <div id="up"></div>
            <div id="down"></div>
        </div>
    </div>
    <script>
       

            // 可以获取到元素的 高度      
        // console.log(up.offsetHeight)  

        // 


       
        
        var l = 300
        setInterval(function(){
            l-= 1
            pipe.style.left = l + 'px'
            if (pipe.offsetLeft <= bird.offsetLeft + bird.offsetWidth) {
                var result = bird.offsetTop < up.offsetHeight || bird.offsetTop > up.offsetHeight + 100 - bird.offsetHeight
                if (result == true) {
                    console.log('撞了')
                }else{
                    console.log('没撞')
                }
            }
        },10)
    </script>
</body>
</html>